import {observable, action, runInAction} from 'mobx';
import dateUtils from '../utils/DateUtils'

/**
 * mobx 状态管理类，所有的逻辑处理都在这里
 * */
class CarPayStore {
    @observable orderInfo; //订单需要的信息
    @observable carInfo; //车辆信息
    @observable months = 1;//月数
    @observable payments = 0;//付款金额
    @observable expirationTime; //到期时间
    @observable renewExpirationTime;//续费后到期时间

    @action
    setOrderInfo = (orderInfo) => {
        runInAction(() => {
            let params = {};
            params.parkId = orderInfo.parkId;
            params.plateNumber = orderInfo.plateNumber;
            postJson("/api/v2/car/anon/queryCarInfo", params, (data) => {
                runInAction(() => {
                    this.carInfo = data;
                    this.orderInfo = orderInfo;
                    this.expirationTime = dateUtils.getExpirationTime(data.eEndTime);
                    this.renewExpirationTime = dateUtils.getNextMonthDay(this.expirationTime, 1);
                    this.payments = orderInfo.monthlyRate * 1;
                    this.months = 1;
                });
            });
        });
    };

    @action
    onChangeMonths = (val) => {
        runInAction(() => {
            this.months = val;
            this.renewExpirationTime = dateUtils.getNextMonthDay(this.expirationTime, this.months);
            this.payments = this.orderInfo.monthlyRate * val;
        });
    };

    paySubmit = (props) => {
        props.history.push('/pages/mobile/car/carPay', {
            payments: this.payments,
            body: {
                plateNumber: this.orderInfo.plateNumber,
                parkId: this.orderInfo.parkId,
                months: this.months,
                renewExpirationTime: this.renewExpirationTime
            }
        })
    };
}

export default new CarPayStore()